package View;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import Controller.Controller;
import Controller.Model;

public class MainMenu extends JPanel implements Observer {
	private static final long serialVersionUID = 1L;
	private final JLabel _title = new JLabel("LTE Multiplexing Simulation");
	private final JButton _check = new JButton("Open");
	private final JTextField _idTextField = new JTextField("Enter Schedule ID");
	private final JLabel imageLTE = new JLabel(new ImageIcon(this.getClass()
			.getResource("/Images/LTE-Logo.png")));
	private final JPanel _panButtons;
	private final JPanel _panCenter;
	private JFileChooser _jFC;
	private final Controller _ctrl;
	private final Model _m;
	private boolean firstClick = true;

	public MainMenu(Controller ctrl, Model m) {
		_ctrl = ctrl;
		_m = m;
		_m.addObserver(this);
		setLayout(new BorderLayout());
		_panButtons = createButtons();
		_panCenter = createCenter();
		add(_panButtons, BorderLayout.SOUTH);
		add(_panCenter, BorderLayout.CENTER);
	}

	private JPanel createCenter() {
		JPanel pan = new JPanel();
		Box b = Box.createVerticalBox();
		b.add(_title);
		b.add(Box.createVerticalStrut(20));
		b.add(imageLTE);
		pan.add(b);

		return pan;
	}

	private JPanel createButtons() {
		JPanel pan = new JPanel();

		GridLayout grid = new GridLayout(1, 2);
		pan.setLayout(grid);
		_check.addActionListener(_ctrl);
		_check.setActionCommand("Check");

		_idTextField.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if (firstClick) {
					_idTextField.setText("");
					firstClick = false;

				}

			}
		});
		pan.add(_idTextField);
		pan.add(_check);

		return pan;
	}

	public String getID() {
		return _idTextField.getText();
	}

	private void loadFile() {
		_jFC = new JFileChooser();
		_jFC.addActionListener(_ctrl);

		_jFC.setDialogTitle("Charger XML");
		FileNameExtensionFilter filter = new FileNameExtensionFilter("xml",
				"xml");
		_jFC.setFileFilter(filter);
		_jFC.setFileView(new XMLFileView());
		_jFC.showDialog(this, "Charger fichier XML");
	}

	@Override
	public void update(Observable o, Object arg) {
		if ((String) arg == "Check")
			loadFile();

	}
}
